<template>
    <el-card class="card f fv">
        <div class="f fac fpj">
            <div class="title">供应情况</div>
        </div>
        <!-- 搜索条件 -->
        <el-form style="margin-top: 0.12rem;" :model="data.searchform" :inline="true">
            <el-form-item label="供应商">
                <el-select style="width: 2rem;" clearable @change="getList(true)" v-model="data.searchform.supplier_id"
                    placeholder="请选择供应商">
                    <el-option v-for="item in data.supplierList" :label="item.name" :value="item.id" />                                 
                </el-select>
            </el-form-item>
            <el-form-item label="产品名称">
                <el-input clearable style="width: 2rem;" @clear="getList(true)" v-model="data.searchform.name" placeholder="请输入产品名称" />
            </el-form-item>
            <el-form-item label="">
                <el-button type="primary" @click="getList(true)">搜索</el-button>
            </el-form-item>
        </el-form>

        <!-- 表格 -->
        <el-table 
            :data="data.tableList" 
            :height="data.tableHeight" 
            v-loading="data.tableLoading" 
            stripe
            class="table">
            <el-table-column prop="supplier_id" align="center" label="供应商">
                <template #default="scope">
                    {{ data.supplierList.filter(item => item.id === scope.row.supplier_id)[0]?.name }}
                </template>
            </el-table-column>              
            <el-table-column prop="name" align="center" label="产品名称" />
            <el-table-column prop="cost_price" align="center" label="供应单价">
                <template #default="scope">
                    <span>￥{{ parseFloat(scope.row.cost_price).toFixed(2) }}</span>
                </template>
            </el-table-column>
            <el-table-column prop="stock_num" align="center" label="供应数量" />
            <el-table-column prop="" align="center" label="供应总价">
                <template #default="scope">
                    <span>￥{{ parseFloat(parseFloat(scope.row.cost_price) * scope.row.stock_num).toFixed(2) }}</span>
                </template>
            </el-table-column>
            <el-table-column prop="unit" align="center" label="单位" />
            <el-table-column prop="create_time" align="center" label="供应日期">
                <template #default="scope">
                    <span>{{ scope.row.create_time && utils.formatDate(new Date(scope.row.create_time).getTime(), 'Y-M-D H:I:S') }}</span>
                </template>
            </el-table-column>
        </el-table>

        <!-- 分页 -->
        <el-pagination background style="margin-top: 0.16rem;" layout="->, total, sizes, prev, pager, next" :pager-count="5"
            v-model:current-page="data.pageNum" v-model:page-size="data.pageSize"
            :total="data.total" @size-change="getList()" @current-change="getList()" />

    </el-card>
</template>

<script setup>
import { ref, onMounted, reactive, watchEffect } from 'vue'
import http from '@/api/home'
import utils from '@/utils/utils'

const refForm = ref()

// 数据集
const data = reactive({
    // 搜索表单对象
    searchform: {},
    // 表格数据
    tableList: [],
    // 表格高度
    tableHeight: 0,
    // 表格加载
    tableLoading: false,
    // 分页 
    pageNum: 1,
    pageSize: 20,
    total: 0,
    // 供应商列表
    supplierList: []
})

// 弹框
const dialogData = reactive({
    // 是否显示
    dialogVisible: false,
    // 标题
    dialogTitle: '',
    // 表单数据 
    form: {},
    // 校验规则
    formRules: {
        
    },
})

onMounted(() => {
    getSupplierList()
    getList()
})

// 获取供应商
const getSupplierList = () => {
    http.productSupplier().then(res => {
        data.supplierList = res.data
    })
}

// 获取列表
const getList = (reset = false) => {
    if (reset) data.pageNum = 1
    data.tableLoading = true
    http.case({
        ...data.searchform,
        pageNum: data.pageNum,
        pageSize: data.pageSize
    }).then(res => {
        data.tableLoading = false
        data.tableList = res.data.records
        data.total = res.data.total
    }).finally(() => {
        data.tableLoading = false
    })
}

// 设置表格高度
const setTableHeight = () => {
    let tableH = 320 // 距离下方的距离 
    let tableHeightDetail = window.innerHeight - tableH
    if (tableHeightDetail <= 400) {
        data.tableHeight = 400
    } else {
        data.tableHeight = window.innerHeight - tableH 
    }
}
watchEffect(() => setTableHeight())

</script>

<style lang="scss" scoped>
.card {
    box-shadow: 0px 1px 4px 0px rgba(0, 0, 0, 0.2);
    margin-top: 0.14rem;
    height: calc(100vh - 0.68rem - 0.7rem);

    .title {
        font-size: 0.2rem;
        font-weight: bold;
        color: #000;
        padding-bottom: 0.1rem;
        border-bottom: 4px solid $ncpColor;
    }

    .role-input {
        width: 2rem;
        margin-right: 0.2rem;
    }

    .table {
        width: 100%;
        .table-sale {
            color: $helpColor;
        }
        .table-info {
            color: #9CA3AF;
            font-size: 0.12rem;
        }   
    }

    .table-edit {
        color: $otherColor;
        margin-right: 0.2rem;
        cursor: pointer;

        &:hover {
            text-decoration: underline;
        }
    }

    .disabled {
        pointer-events: none;
        opacity: 0.8;
    }

    .table-delete {
        color: $helpColor;
        cursor: pointer;

        &:hover {
            text-decoration: underline;
        }
    }

    :deep(.el-drawer__title) {
        font-size: 0.16rem;
    }
    .content {
        position: relative;
        height: 100%;
        .upload-img .img {
            width: 1rem;
            height: 1rem;
            display: block;
        }
        :deep(.upload-img .el-upload) {
            border: 1px dashed var(--el-border-color);
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
            transition: var(--el-transition-duration-fast);
            &:hover {
                border-color: var(--el-color-primary);
            }
        }
        .el-icon.img-uploader-icon {
            font-size: 0.28rem;
            color: #8c939d;
            width: 1rem;
            height: 1rem;
            text-align: center;
        }

        .footer {
            position: absolute;
            bottom: 0;
            left: 0;
        }
    }

}
</style>